package com.bls.cate.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ScreenUtils;
import com.bls.cate.model.ArticleListRes;
import com.bls.cate.ui.activity.read_article.ReadArticleActivity;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/4/3.
 */

public class ReadArticlePageAdapter extends PagerAdapter {
    private static final int Screen_Width = ScreenUtils.getScreenWidth();
    private List<ArticleListRes.ResultListDataBean.ArticleImageListBean> articleImageList;
    private ReadArticleActivity mActivity;

    public ReadArticlePageAdapter(ReadArticleActivity mActivity, List<ArticleListRes.ResultListDataBean.ArticleImageListBean> articleImageList) {
        this.mActivity = mActivity;
        this.articleImageList = articleImageList;
    }

    @Override
    public int getCount() {
        return articleImageList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ArticleListRes.ResultListDataBean.ArticleImageListBean bean = articleImageList.get(position);
        double ratio = (double) bean.getWidth() / (double) bean.getHeight();
        int imageItemHeight = (int) (Screen_Width / ratio);
        final ImageView imageItem = new ImageView(mActivity);
        imageItem.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(Screen_Width, imageItemHeight);
        imageItem.setLayoutParams(layoutParams);
        Glide.with(mActivity).load(bean.getImagePath()).crossFade().into(imageItem);
        container.addView(imageItem);
        return imageItem;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
